import axios from 'axios'
import store from './store/store'
import * as types from './store/types'
import router from './router'
// import getcookie from './util/getcookie'

// axios 配置
axios.defaults.timeout = 5000
// axios.defaults.timeout = 120000
// axios.defaults.baseURL = '/' // liu后端
// axios.defaults.baseURL = '/' // 正式环境
// axios.defaults.baseURL = 'http://localhost:8081/' // 本机
// axios.defaults.baseURL = 'http://selection.e0316.com:8088/' // 本机
// axios.defaults.baseURL = 'http://localhost/' // mock
axios.defaults.baseURL = '/' // liu

// axios.defaults.baseURL = '/' // 正式环境
// axios.defaults.baseURL = 'http://localhost/' // mock
// axios.defaults.baseURL = 'http://192.168.1.11:8080/' // 本机后端
// axios.defaults.baseURL = 'http://192.168.2.180:8080/' // 笔记本后端
// axios.defaults.withCredentials = true
axios.defaults.selectionAPI = 'd'
axios.defaults.youngAPI = 'y'
axios.defaults.analysisAPI = 'a'
axios.defaults.unitReportAPI = 'u'
axios.defaults.workRecordAPI = 'r'
axios.defaults.personListAPI = 'l'
axios.defaults.fileDistributionAPI = 'f'
axios.defaults.shenghuohuiAPI = 'm'
axios.defaults.trainingAPI = 't'
axios.defaults.meetingAPI = 'e'
axios.defaults.zbbsmzAPI = 'z'
axios.defaults.jinshengAPI = 'j'
axios.defaults.recruitAPI = 'zp'
axios.defaults.postAPI = 'p'
axios.defaults.salaryAPI = 's'
axios.defaults.headcountAPI = 'h'
axios.defaults.supervisionAPI = 'jt'

// if (window.location.protocol === 'http:') {
//   axios.defaults.baseWsURL = 'ws://' + window.location.host + '/z'
//   // axios.defaults.baseWsURL = 'ws://localhost:8090/z' //websocket本地测试时需要打开本句
// } else if (window.location.protocol === 'https:') {
//   axios.defaults.baseWsURL = 'wss://' + window.location.host + '/z'
//   // axios.defaults.baseWsURL = 'wss://localhost:8090/z' //websocket本地测试时需要打开本句
// }

// http request 拦截器
axios.interceptors.request.use(
  config => {
  // 允许携带cookie ,这个是解决跨域产生的相关问题
    config.withCredentials = true
    return config
  },
  err => {
    return Promise.reject(err)
  }
)

// http response 拦截器
axios.interceptors.response.use(
  response => {
    // console.log('response.status')
    // console.log(response.status)
    // console.log('response.data')
    // console.log(response.data)
    if (response.status === 403 || response.data === 401) {
      // 401 清除token信息并跳转到登录页面
      store.commit(types.LOGOUT)
      console.log('未授权 清除token信息并跳转到登录页面')

      // 只有在当前路由不是登录页面才跳转
      router.currentRoute.name !== 'login' &&
      router.replace({
        name: 'login',
        query: { redirect: router.currentRoute.path }
      })
    }
    return response
  },
  error => {
    console.log('403错误提示:')
    console.log(error.response)
    if (error.response) {
      console.log('error.response.status:' + error.response.status)
      switch (error.response.status) {
        case 403:
          // 返回 403 清除token信息并跳转到登录页面
          store.commit(types.LOGOUT)
          router.currentRoute.name !== 'login' &&
          router.replace({
            name: 'login',
            query: { redirect: router.currentRoute.path }
          })
      }
    }
    console.log('http response 拦截器错误' + JSON.stringify(error)) // console : Error: Request failed with status code 402
    return Promise.reject(error.response)
  }
)

export default axios
